package com.storyroad.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.storyroad.utils.DBConnection;
import com.storyroad.utils.TableNames;

/**
 * Servlet implementation class ReportStory
 */
public class ReportStory extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ReportStory() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * A story fragment is reported to the system by a registered user. Users can report story fragments to the system without
	 * specifying a reason however it is not removed from the system until the admin decides to remove it.
	 * @param request HttpServletRequest object that contains data coming from front-end
	 * @param response HttpServletResponse object that contains data which is prepared after processing the incoming data
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		int f_storyId = Integer.parseInt(request.getParameter("story_id"));
		String f_requestType = request.getParameter("request_type");

		if (f_requestType == null || !f_requestType.equals("Android")) {
			f_requestType = "Web";
		}

		Connection conn = new DBConnection().connect();
		PrintWriter out = response.getWriter();
		if (conn == null) {
			out.println("Connection error");
		} else {
			try {
				request.setCharacterEncoding("utf8");
				response.setContentType("application/json");
				PreparedStatement stmt = conn.prepareStatement("INSERT INTO " + TableNames.REPORT_TABLE + "(s_id) VALUES (?)");
				stmt.setInt(1, f_storyId);

				int databaseResponse = stmt.executeUpdate();

				boolean reportSuccess = false; // as default
				if (databaseResponse == 1) {
					reportSuccess = true;
				} else if (databaseResponse == 0) {
					reportSuccess = false;
				} else {
					// TODO DO NOTHING!
				}

				if (f_requestType.equals("Android")) {
					JSONObject reportResultJsonObject = new JSONObject();
					try {
						reportResultJsonObject.put("reportSuccess",	reportSuccess);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					out.print(reportResultJsonObject.toString());
				} else if (f_requestType.equals("Web")) {
					// Redirect to the reported story fragment's page after operation is completed.
					request.setAttribute("reportSuccess", reportSuccess);
					request.getRequestDispatcher("ReadStory?story_id=" + f_storyId).forward(request, response);
				} else {
					// TODO DO NOTHING!
				}

			} catch (SQLException e) {
				out.print(e.getMessage());
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
